package com.kotlin.android.app.ui.splash

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import com.kotlin.android.app.KEY_FLASH_AD
import com.kotlin.android.app.MainTabActivity
import com.kotlin.android.app.R
import com.kotlin.android.app.constant.CITY_ID
import com.kotlin.android.app.constant.KEY_IS_FROM_GUIDE
import com.kotlin.android.app.data.common.FlashAd
import com.kotlin.android.app.databinding.ActSplashBinding
import com.kotlin.android.core.BaseVMActivity
import com.kotlin.android.core.BaseViewModel
import com.kotlin.android.ktx.ext.click.onClick
import com.kotlin.android.ktx.ext.core.setBackground
import com.kotlin.android.ktx.ext.core.visible
import com.kotlin.android.ktx.ext.datastore.DataStoreExt
import com.kotlin.android.ktx.ext.dimension.dpF
import com.kotlin.android.ktx.ext.dimension.screenHeight
import com.kotlin.android.ktx.ext.dimension.screenWidth
import com.kotlin.android.ktx.ext.immersive.immersive
import com.kotlin.android.ktx.ext.log.e
import com.kotlin.android.ktx.lifecycle.KtxActivityManager
import com.kotlin.android.media.image.coil.loadImage
import com.kotlin.android.router.RouterManager
import java.util.Timer
import java.util.TimerTask

/**
 * 闪屏广告：
 *
 *
 */
class SplashActivity : BaseVMActivity<BaseViewModel, ActSplashBinding>() {
    private val timerTime = 1000L // 倒计时间隔/延时
    private var countdown = 4
    private val timer: Timer by lazy { Timer() }
    private var flashAd: FlashAd.Objects? = null

    override fun getIntentData(intent: Intent?) {
        super.getIntentData(intent)
        intent?.apply {
            flashAd = getSerializableExtra(KEY_FLASH_AD) as? FlashAd.Objects
        }
    }

    override fun initTheme() {
        super.initTheme()
        immersive()
            .transparentStatusBar(isFitsSystemWindows = false)
            .statusBarDarkFont(false)
    }

    override fun initView() {
        mBinding?.skipTv?.apply {
            visible()
            setBackground(
                colorRes = R.color.color_60000000,
                cornerRadius = 14.dpF
            )
            onClick {
                timer.cancel()
                openNext()
            }
        }
    }

    override fun initData() {
        flashAd?.apply {
            mBinding?.splashIv?.loadImage(
                data = R.drawable.splash,
                width = screenWidth,
                height = screenHeight,
            )
            updateCountdown()
            timer.schedule(task, timerTime, timerTime)
        } ?: openNext()
    }

    override fun startObserve() {
    }

    override fun onBackPressed() {
        super.onBackPressed()
        KtxActivityManager.exitApp()
    }

    /**
     * 广告页倒计时
     */
    private var task: TimerTask = object : TimerTask() {
        override fun run() {
            runOnUiThread {
                countdown--
                updateCountdown()
                if (countdown <= 0) {
                    timer.cancel()
                    openNext()
                }
            }
        }
    }

    /**
     * 更新倒计时
     */
    private fun updateCountdown() {
        mBinding?.skipTv?.text = getString(R.string.skip, countdown.toString())
    }

    /**
     * 打开下一个页面
     */
    private fun openNext() {
        openMainTab()
    }

    /**
     * 打开主页
     */
    private fun openMainTab() {
        // openHome() 直接访问跳转。（不需要路由，因为app冷启动路由组件初始化耗时，会出现页面加载等待的问题）
        startActivity(Intent(this, MainTabActivity::class.java))
        finishDelay()
    }

    /**
     * 启动页延伸销毁，保持页面衔接。
     */
    private fun finishDelay() {
        Looper.getMainLooper()?.apply {
            Handler(this).postDelayed({
                finish()
            }, 3000)
        }
    }
}